/**
 * @Author:      王秀民 
 * @DateTime:    2019-01-11 17:09:33
 * @remarks:    	  
 */
// 引入域名
import Url from '../../assets/js/domain.js'
import { notification } from 'antd'
import history from '../../assets/js/history.js'

const openNotification = (type, msg) => {
  notification.config({
    placement: 'topRight',
    duration: 3
  })
  notification[type]({
    message: '温馨提示',
    description: msg
  })
}
// 面包屑方法
export function setBreadcrumb(data) {
  return (dispatch, getState) => {
    dispatch({ type: 'SET_BREADCRUMB', data: data })
  }
}
// 登录方法
export function login(name, password) {
  let formdata = new FormData();
  formdata.append("userName", name);
  formdata.append("passWord", password);
  return (dispatch, getState) => {
    window.fetch(Url + '/login', {
      method: 'post',
      header: {
        "Content-Type": "application/json"
      },
      body: formdata
      // body:JSON.stringify({
      // 	"userName":name,
      // 	"passWord":password
      // })
    }).then(res => {
      return res.json()
    }).then(res => {
      if (res.code === -2) {
        openNotification('warning', '用户不存在！请输入正确的用户名和密码！');
      } else if (res.code === -1) {
        openNotification('warning', '您的输入为空！请输入正确的用户名和密码！');
      } else if (res.code === -3) {
        openNotification('warning', '您的密码输入错误！请输入正确的密码！');
      } else if (res.code === -4) {
        openNotification('warning', '此用户禁止登录！');
      } else if (res.code === -1000001) {
        openNotification('error', '系统错误！');
      } else if (res.code === 200) {
        // dispatch({ type: 'LOGIN', data: res.data })
        sessionStorage.setItem("staffId",res.data.staffId);
        sessionStorage.setItem("staffName",res.data.staffName);
        sessionStorage.setItem("token",res.data.token);
        sessionStorage.setItem("wuYeId",res.data.wuYeId);
        sessionStorage.setItem("xiaoQuId",res.data.xiaoQuId);
        sessionStorage.setItem("xiaoQuName",res.data.xiaoQuName);
        dispatch({ type: 'LOGIN', data: true })
        openNotification('success', '登录成功');
        setTimeout(function() {
          history.push('/index'); //这里
        }, 3000)
      }
    })
  }
}